<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2017-03-17
 * Time: 15:37
 */

namespace app\src\sunsun\filterVat\action;
use app\src\sunsun\BaseSunsunAction;
use app\src\sunsun\filterVat\helper\FilterVatEncodeHelper;
use app\src\sunsun\filterVat\logic\FilterVatDeviceLogic;
use sunsun\filter_vat\req\FilterVatDeviceInfoReq;


/**
 * Class FilterVatDeviceInfoAction
 * 获取设备信息
 * @package app\src\sunsun\filterVat
 */
class FilterVatDeviceInfoAction extends BaseSunsunAction
{
    /**
     * 获取设备信息
     * @param $did
     * @return array
     */
    public function deviceInfo($did){
        $req = new FilterVatDeviceInfoReq();
        $req->setSn(intval(time()));

        $data = FilterVatEncodeHelper::encode($did,$req->toDataArray());
        $this->sendToClient($did,$data);
        $result = (new FilterVatDeviceLogic())->getInfo(['did'=>$did]);
        $result['info'] = $this->processDevice($result['info']);
        return $result;
    }

    private function processDevice($info){
        if(!is_array($info)){
            return $info;
        }
        //增加一个设备断开时间
        $update_time = strtotime($info['update_time']);
        $hb = $info['hb'];
        //3倍的心跳时间作为设备断开判断依据
        //相当于重试3次
        $info['logout_time'] = $update_time + 3*$hb;
        $info['is_disconnect'] = 0;
        $info['is_disconnect'] = 0;
        if($info['logout_time'] < time()){
            $info['is_disconnect'] = 1;
        }
        unset($info['logout_time']);
        return $info;
    }
}